package com.fighYo.trace.controller;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.*;
import com.fighYo.trace.service.InfoService;
import com.fighYo.trace.service.QrcodeService;
import com.fighYo.trace.service.ScanService;
import com.fighYo.trace.service.UserService;
import com.fighYo.trace.utils.QRUtil;
//import com.fighYo.trace.utils.QRUtils;
import com.fighYo.trace.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2022-04-15
 */
@CrossOrigin
@Api(tags = {"溯源码管理"})
@RestController
@RequestMapping("/qrcode")
public class QrcodeController {

    @Autowired
    private InfoService infoService;

    @Autowired
    private UserService userService;

    @Autowired
    private QrcodeService qrcodeService;

    @Autowired
    private QRUtil qrUtil;

    @Autowired
    private ScanService scanService;

    @ApiOperation("总数")
    @GetMapping("count")
    public R count() {
        int count = qrcodeService.count(null);
        return R.ok().data("item", count);
    }

    @ApiOperation("根据内容生成溯源码 测试用")
    @PostMapping("create")
    public R createQR(String content) {
        try {
            String url = qrUtil.createQR(content);
            return R.ok().data("url", url);
        } catch (Exception ex) {
            return R.error().msg("生成溯源码失败");
        }
    }

    @ApiOperation("根据订单号查询溯源码")
    @GetMapping("listByPaymentId")
    public R getByPaymentId(String paymentId) {
        QueryWrapper<Qrcode> wrapper = new QueryWrapper<>();
        wrapper.eq("payment_id", paymentId);
        List<Qrcode> list = qrcodeService.list(wrapper);
        return R.ok().data("list", list);
    }

    @ApiOperation(("根据UUID查询溯源码溯源信息"))
    @GetMapping("getByUUID")
    R getByUUID(String uuid, String ip, String loc) {

        Date date = new Date();

        Scan scan = new Scan();
        scan.setQrcodeId(uuid);
        scan.setLoc(loc);
        scan.setIp(ip);
        scan.setHappenTime(date);

        Qrcode qrcode = qrcodeService.getQrcodeByUUID(uuid);

        if (qrcode == null) {
            scan.setResult("2");
            scanService.save(scan);
            return R.error().msg("未能查询到改溯源码有效信息");
        }

        String userId = qrcode.getUserId();
        Info userInfo = infoService.getUserInfo(userId);

        if (qrcode.getFirstTime() == null) {
            qrcode.setFirstTime(date);
            qrcode.setSearchTime(1);
            qrcode.setFirstIp(ip);
            qrcode.setFirstLoc(loc);
            scan.setResult("0");
        } else {
            scan.setResult("1");
            qrcode.setSearchTime(qrcode.getSearchTime() + 1);
        }
        scanService.save(scan);
        qrcodeService.updateById(qrcode);
        return R.ok().data("info", userInfo).data("qrcode", qrcode);

    }

}

